package com.woniuxy.usercoachassemble.controller;


import com.woniuxy.usercoachassemble.entity.Account;
import com.woniuxy.usercoachassemble.entity.Userinfo;
import com.woniuxy.usercoachassemble.service.AccountService;
import com.woniuxy.usercoachassemble.service.UserinfoService;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-10
 */
@RestController
@RequestMapping("account")
public class AccountController {
    @Autowired
    private AccountService accountServiceImpl;

    @Autowired
    private UserinfoService userinfoService;

    //查询账户
    @RequestMapping("getaccount")
    public Account selectaccountbyid(HttpServletRequest request, int userId) {
        return accountServiceImpl.selectaccountbyid(request.getIntHeader("userId"));
    }

    // 给当前账号充值充值
    @PostMapping("recharge")
    public Result recharge(Integer money, HttpServletRequest request) {

        if (money == null || money == 0) {
            return Result.Error(600, "充值余额格式错误!");
        }

        Integer userId = Integer.parseInt(request.getHeader("userId"));
        // 查出userinfo
        Userinfo userinfo = userinfoService.query().eq("user_id", userId).one();

        Account account = accountServiceImpl.getById(userinfo.getAcccountId());
        account.setMoney(account.getMoney() + money);
        accountServiceImpl.updateById(account);

        Result success = Result.Success();
        success.setMessage("充值成功");
        return success;
    }

}

